package com.ruoyi.common.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.service.ConfigService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.erecord.employee.domain.vo.EmployeeUserVo;
import com.ruoyi.erecord.employee.service.IEmployeeService;
import com.ruoyi.erecord.region.domain.bo.RegionBo;
import com.ruoyi.erecord.region.service.IRegionService;
import com.ruoyi.erecord.vehicles.domain.vo.VehiclesNameVo;
import com.ruoyi.erecord.vehicles.service.IVehiclesService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.NumberUtil;
import lombok.RequiredArgsConstructor;

/**
 * 电子档案 通用查询controller
 * 
 * @author cc
 *
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/common")
public class ERcordCommonController {
	private final ISysDeptService deptService;
	private final ISysPostService postService;
	private final ConfigService configService;

	private final IEmployeeService employeeService;
	private final IVehiclesService vehiclesService;
	private final IRegionService regionService;

	/**
	 * 获取部门、岗位列表
	 * 
	 * @param queryDept 是否查询部分数据，默认 true
	 * @param queryPost 是否查询岗位数据，默认 true
	 * @return map { "posts" : List<SysPost> , "depts" : List<SysDept>}
	 */
	@GetMapping("/getDeptAndPost")
	public R<Map<String, Object>> getDeptAndPost(
			@RequestParam(name = "queryDept", defaultValue = "true") Boolean queryDept,
			@RequestParam(name = "queryPost", defaultValue = "true") Boolean queryPost) {
		Map<String, Object> result = new HashMap<>();

		if (queryPost) {
			List<SysPost> posts = postService.selectPostAll();
			result.put("posts", posts);
		}

		if (queryDept) {
			long deptId = NumberUtil
					.parseInt(this.configService.getConfigValue(ERecordConstant.CONFIG_EMPLOYEE_DEPTS_ID));
			SysDept dept = new SysDept();
			dept.setParentId(deptId);
			List<SysDept> depts = deptService.selectDeptList(dept);
			result.put("depts", depts);
		}

		return R.ok(result);
	}

	/**
	 * 模糊查询 人员列表
	 * 
	 * @param name 名称
	 * @return
	 */
	@GetMapping("/getEmpUserByName")
	public R<List<EmployeeUserVo>> getEmpUserByName(@RequestParam(name = "name", defaultValue = "") String name) {
		return R.ok(this.employeeService.getEmployeeUserVoList(name));
	}

	/**
	 * 模糊查询 车辆列表
	 * 
	 * @param number 车牌号
	 * @return
	 */
	@GetMapping("/getVehiclesByNumber")
	public R<List<VehiclesNameVo>> getVehiclesByNumber(
			@RequestParam(name = "number", defaultValue = "") String number) {
		return R.ok(this.vehiclesService.getVehiclesNameVoList(number));
	}

	@GetMapping("/getRegionTree")
	public R<List<Tree<String>>> getRegionTree(RegionBo bo) {
		return R.ok(regionService.treeList(bo));
	}
}
